namespace DotNetNuke.HttpModules
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Services.Personalization;
    using System;
    using System.Web;

    public class PersonalizationModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication application)
        {
            application.EndRequest += new EventHandler(this.OnEndRequest);
        }

        public void OnEndRequest(object s, EventArgs e)
        {
            HttpContext Context = ((HttpApplication) s).Context;
            if (Context.Request.IsAuthenticated)
            {
                PortalSettings _portalSettings = (PortalSettings) Context.Items["PortalSettings"];
                if (_portalSettings != null)
                {
                    UserInfo UserInfo = UserController.GetCurrentUserInfo();
                    if (UserInfo.UserID != -1)
                    {
                        new PersonalizationController().SaveProfile(Context, UserInfo.UserID, _portalSettings.PortalId);
                    }
                }
            }
        }

        public string ModuleName
        {
            get
            {
                return "PersonalizationModule";
            }
        }
    }
}

